package com.example

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.example.entity.User
import com.example.mapper.UserMapper
import com.example.service.UserServiceImp
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@SpringBootTest
// 注入Service实现类而不是接口
class UserApplicationTests @Autowired constructor(
    private val userService: UserServiceImp, private val userMapper: UserMapper
) {

    @Test
    fun contextLoads() {
    }

    @Test
    fun testListUser() = userService.list(null).forEach { println(it) }

    @Test
    fun testPagination() {
        val userPage = userService.page(Page(1, 3))
        userPage.records.forEach { println(it) }
        assertTrue { userPage.hasNext() }
        assertFalse { userPage.hasPrevious() }
        assertEquals(5, userPage.total)
    }

    @Test
    fun testSaveUser() = assertTrue { userService.save(User(username = "小小兔", age = 5, email = "xiaoxiaotu@qq.com")) }

    @Test
    fun testSaveBatchUser() = assertTrue {
        userService.saveBatch(
            listOf(
                User(username = "小小兔1", age = 5, email = "xiaoxiaotu@qq.com"),
                User(username = "小小兔2", age = 5, email = "xiaoxiaotu@qq.com"),
                User(username = "小小兔3", age = 5, email = "xiaoxiaotu@qq.com"),
            )
        )
    }

    @Test
    fun testSelectByIdUser() = assertEquals(
        User(1, "John", 18, "test1@baomidou.com"), userService.getById(1)
    )

    @Test
    fun testListByIdsUsers() = userService.listByIds(listOf(1, 2, 3, 4, 5)).forEach { println(it) }

    @Test
    fun testSelectByMapUser() {
        assertTrue { userService.save(User(username = "John", age = 18, email = "john@qq.com")) }
        userService.listByMap(mapOf("name" to "John", "age" to 18)).forEach { println(it) }
    }

    @Test
    fun testUpdateUser() = assertTrue { userService.updateById(User(id = 3, username = "Helen")) }

    @Test
    fun `selectAllAge`() = userService.selectAllAge(20).forEach { println(it) }

    @Test
    fun testTableLogic() = assertTrue { userService.removeById(2) }

    @Test
    fun testSelectByAge() = userMapper.selectPageByAge(Page(1, 3), age = 18).forEach { println(it) }

    @Test
    fun testWrapper() = userService.selectByNameBetweenAge().forEach { println(it) }

    @Test
    fun testSelectMaps() = userService.selectMaps().forEach { println(it) }

    @Test
    fun testSelectInSql() = userService.selectInSql().forEach { println(it) }

}
